<?php

namespace app\admin\controller;

use app\common\model\JieChi as JieChiModel;
use app\common\controller\AdminBase;
use think\Request;


class JieChi extends AdminBase
{

    protected $model;

    protected function _initialize()
    {
        parent::_initialize();
        $this->model =new JieChiModel();
        $data_lists  = $this->model->where(['uid'=>session('admin_id')])->order(['sort' => 'ASC'])->select();
        $this->assign('data_lists', $data_lists);
    }

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(){
        return view();
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create($style_id)
    {
        switch ($style_id) {
            case 1:
                return view('style1');
                break;

            case 2:
                return view('style2');
                break;

            case 3:
                return view('style3');
                break;
            case 4:
                return view('style4');
                break;
            case 5:
                return view('style5');
                break;
            default:
                # code...
                break;
        }
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        if ($this->request->isPost() && $this->request->post('type') == 'edit') {
            $id    = $this->request->post('id');
            $data  = $this->request->param();
            if ($this->model->allowField(true)->save($data, $id) !== false) {
                $this->success('更新成功');
            } else {
                $this->error('更新失败');
            }
            exit;
        }else{
            if($request->isPost()){
                if($this->model->data($request->post(),true)->allowField(true)->save()){
                    $this->success('保存成功');
                }else{
                    $this->error('保存失败');
                }
            }
        }

    }



    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        $data = $this->model->where(['uid'=>session('admin_id')])->find($id);
        // var_dump($data->toarray());
        return $this->fetch('style'.$data['style'], ['data' => $data->toarray()]);
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        if ($this->model->destroy($id)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }


    public function updateSort(){
        $data = request()->only(['sort'],'post');
        $data = explode(',',trim($data["sort"]));
        $datas = [];
        foreach($data as $key =>$val){
            $datas[$key]['id'] = $val;
            $datas[$key]['sort'] = $key;
        }
        if($this->model->saveAll($datas)){
            return ['code'=>1,'msg'=>'排序成功！'];
        }else{
            return ['code'=>-1,'msg'=>'排序失败！'];
        }
    }
}
